<template>
  <div class="jump-scb-player">
    <!-- {{ scb.devNow }} -->
    <!-- 公共头部 -->
    <page-title
      v-if="!['welcome'].includes(tplCode)"
      :devTime="scb.devNow"
      :tpl-code="tplCode"
      :match-name="matchName"
      :event-name="eventName"
    />

    <!-- 欢迎页面 -->
    <welcome v-if="tplCode === 'welcome'" />

    <!-- 个人信息、成绩公共 -->
    <result
      v-else-if="tplCode === 'result'"
      :baseInfo="baseInfo"
      :dynamicInfo="dynamicInfo"
      :currentRank="currentRank"
      :rank3="rank3"
    />

    <!-- 名次页面 -->
    <rank ref="rankRef" v-else-if="tplCode === 'rank'" :rec-time="localTime" />

    <!-- 今日赛程 -->
    <schedule v-else-if="tplCode === 'schedule'" />
  </div>
</template>

<script>
import { api, TWsClient } from '@/assets/api'
import { TJumpScb } from './jumpScb.js'
import { TTsJumpCore } from './jumpTsUtils'
import Welcome from '@/components/scb/Welcome.vue'
import Result from '@/components/scb/Result.vue'
import PageTitle from '@/components/scb/Title.vue'
import Rank from '@/components/scb/Rank.vue'
import Schedule from '@/components/scb/Schedule.vue'
var wsClient = new TWsClient()
export default {
  mounted() {
    /*
    var netTimeSecond = 230.999999999999999999999;
    var r= (0.1 * Math.ceil(netTimeSecond * 10)).toFixed(1);
    console.log(netTimeSecond , r)
    */

    var me = this
    if (me.timer) {
      window.clearInterval(me.timer)
      me.timer = null
    }
    try {
      me.$nextTick()
        .then((_) => {
          return me.init()
        })
        .then((_) => {
          me.timer = window.setInterval(() => {
            me.interval()
          }, 20)
        })
    } catch (error) {
      console.log(error)
    }
  },
  components: {
    Welcome,
    PageTitle,
    Result,
    Rank,
    Schedule,
  },
  data() {
    return {
      timer: null,
      pageStateKey: 'jumpTs.scbPlayer.stateKey',
      pageState: {},
      scb: new TJumpScb(),
      nowTm: 0,
      tplCode: 'welcome',
      localTime: 0,
      matchInfo: {},
      matchName: '',
      eventName: '',
      resultInfo: {},
      times: {},
      scoreObj: {},
      baseInfo: [],
      dynamicInfo: [],
      currentRank: [],
      rank3: [],
    }
  },
  watch: {
    tplCode(newVal, oldVal) {
      if (oldVal === 'rank' && newVal !== 'rank' && this.$refs.rankRef) {
        console.log('--------------------------')
        this.$refs.rankRef.clearTimer?.()
      }
    },
  },
  methods: {
    init() {
      var me = this
      return new Promise((su, fa) => {
        try {
          me.pageState = api.getSessionObj(me.pageStateKey, me.pageState)
          me.scb.api = api
          console.log(me.scb)
          me.initWsClient()
          su()
        } catch (error) {
          console.log(error)
          su()
        }
      })
    },
    initWsClient() {
      var me = this
      try {
        if (wsClient != null) {
          wsClient.forceCloseFlag = true
          wsClient.disconnect()
          wsClient = null
        }
        wsClient = new TWsClient()
        var onConnectedChanged = (sender, data) => {
          var me = this
          try {
            console.log('onConnectedChanged', data, wsClient.settings.wsUrl)
            if (data) {
              wsClient.sendMsg('regist', { srcId: wsClient.peerId })
            }
          } catch (error) {
            console.log(error)
          }
        }
        var onSessionChanged = (sender, data) => {
          var me = this
          try {
            me.sessions = data
            // console.log('onSessionChanged', data)
          } catch (error) {
            console.log(error)
          }
        }
        var onMsg = (sender, msg) => {
          var me = this
          try {
            if (msg.data.msgType == 'SignalCode') {
              console.log('jump msg', msg)
              var sc = msg.data.msgData.s
              var tm = msg.data.msgData.t
            } else if (msg.data.msgType == 'RunningTime') {
              var devTime = msg.data.msgData.t
              me.scb.setDevTime(devTime)
            } else if (msg.ac == 'feiScb') {
              me.scb.setMsg(msg)
              // console.log('-------------')
              console.log('----------------', me.scb)
              me.localTime = me.scb.times.recTsTime
              // me.matchName = me.scb.dispMatchName
              // me.eventName = me.scb.dispEventName
              // console.log(me.matchInfo)
              me.resultInfo = me.scb.scbData.resultInfo
              me.times = me.scb.times
              me.tplCode = msg.data.tplCode
            }
          } catch (error) {
            console.log(error)
          }
        }
        api.callApi('equTsV1_obj', 'getObj', { objKey: 'tsEquSettings' }).then((re) => {
          try {
            if (re.status == 1) {
              var wsUrl = re.result.wsUrl
              console.log(wsUrl)
              wsClient.init(
                wsUrl,
                'tsJumpScb2025SH',
                ['regist', 'sessionChanged', 'feiScb'],
                onConnectedChanged,
                onSessionChanged,
                onMsg,
                { clientType: '障碍赛大屏' }
              )
              wsClient.connect()
            }
          } catch (err) {
            console.log(err)
          }
        })
      } catch (error) {
        console.log(error)
      }
    },
    interval() {
      var me = this
      try {
        var devTm = me.scb.devNow
        me.scb.setDevTime(devTm)
        me.matchName = me.scb.dispMatchName
        me.eventName = me.scb.dispEventName
        if (me.tplCode === 'result') {
          // 基础数据
          me.baseInfo = [
            {
              name: '项目名称',
              code: 'dispEventName',
              value: me.scb.dispEventName,
              remark: '-',
            },
            {
              name: '场次名称',
              code: 'dispMatchName',
              value: me.scb.dispMatchName,
              remark: '-',
            },
            {
              name: '运动员编号',
              code: 'dispOrder',
              value: me.scb.dispOrder,
              remark: '-',
            },
            {
              name: '运动员名称',
              code: 'dispAthName',
              value: me.scb.dispAthName,
              remark: '-',
            },
            {
              name: '代表团名称',
              code: 'dispOrgName',
              value: me.scb.dispOrgName,
              remark: '-',
            },
            {
              name: '马编号',
              code: 'dispHorseBib',
              value: me.scb.dispHorseBib,
              remark: '-',
            },
            {
              name: '马名称',
              code: 'dispHorseName',
              value: me.scb.dispHorseName,
              remark: '-',
            },
            {
              name: '比赛总人数',
              code: 'dispResultNum',
              value: me.scb.dispResultNum,
              remark: '-',
            },
          ]

          // 动态数据
          me.dynamicInfo = [
            {
              name: '时钟、判罚',
              code: 'dispClockSecond',
              value: me.scb.dispClockSecond,
              remark: '-',
            },
            {
              name: '障碍罚分',
              code: 'dispPenPoint',
              value: me.scb.dispPenPoint,
              remark: '-',
            },
            {
              name: '时间罚分',
              code: 'dispPenTime',
              value: me.scb.dispPenTime,
              remark: '-',
            },
            {
              name: '矫正时间',
              code: 'dispPenSecond',
              value: me.scb.dispPenSecond,
              remark: '-',
            },
            {
              name: '总罚分',
              code: 'dispPenTotal',
              value: me.scb.dispPenTotal,
              remark: '-',
            },
            {
              name: '当前排名',
              code: 'dispRealRank',
              value: me.scb.dispRealRank,
              remark: '-',
            },
            {
              name: '超第一名时间',
              code: 'dispDltSecond',
              value: me.scb.dispDltSecond,
              remark: '-',
            },
          ]

          // 排名数据
          me.currentRank = me.scb.dispCurrentRank

          me.rank3 = me.scb.dispRank3
        }
      } catch (error) {
        console.log(error)
      }
    },

    getDisp45() {
      var me = this
      var res = 45
      try {
        res = res + 0.001
      } catch (er) {
        console.log(er)
      }
      return res
    },

    getCurrentStatus() {
      var me = this
    },
  },
}
</script>

<style lang="less" scoped>
.jump-scb-player {
  width: 100vw;
  height: 100vh;
  // overflow: hidden;
  font-family: 'msyhbd';
}
</style>
